package com.wuye.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wuye.common.vo.PageInfo;
import com.wuye.common.vo.ParkingUpdateInfo;
import com.wuye.common.vo.RetVO;
import com.wuye.common.vo.StatVO;
import com.wuye.constants.BaseConstants;
import com.wuye.entity.User;
import com.wuye.services.ParkingServiceManager;

public class ParkingAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6732943994983080207L;
	private Logger log=Logger.getLogger(ParkingAction.class);
	@Autowired
	private ParkingServiceManager parkingServiceManager;
	
	@Setter
	@Getter
	private int page;
	@Setter
	@Getter
	private String inParam;
	@Setter
	@Getter
	private String suc_info;
	
	public String getParking()
	{
		JSONObject json = null;
		
		try
		{
			json = JSONObject.fromObject(inParam);
			int parkingId = Integer.parseInt(json.getString("parkingId"));
			ParkingUpdateInfo parkingUpdateInfo = parkingServiceManager.getParkingUpdateInfo(parkingId);
			
			json.clear();
			json.put("found", "1");
			
			if (parkingUpdateInfo != null)
			{
				json.put("data", JSONArray.fromObject(parkingUpdateInfo).get(0));
			}
			else
			{
				json.put("found", "0");
			}			
			
		} 
		catch (Exception e)
		{
			json.clear();
			json.put("found", "0");
			log.debug(e.getStackTrace());
		}
		
		suc_info = json.toString();
		
		return SUCCESS;
	}
	
	public String saveParking()
	{
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("ok", "1");
		
		try 
		{
			JSONObject json = JSONObject.fromObject(inParam);
			JSONArray jsonArray = json.getJSONArray("data");
			
			if (jsonArray != null && jsonArray.size() > 0)
			{
				parkingServiceManager.addParking(jsonArray.getJSONObject(0));				
			}
			
		} 
		catch (Exception e) 
		{
			map.put("ok", "0");
			log.info(e.getMessage());
		}
		finally
		{
			JSONObject jsontip = JSONObject.fromObject(map);
			suc_info = jsontip.toString();
		}
		
		return SUCCESS;
	}
	
	public String updateParking()
	{
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("ok", "1");
		
		try 
		{
			JSONObject json = JSONObject.fromObject(inParam);
			JSONArray jsonArray = json.getJSONArray("data");
			
			if (jsonArray != null && jsonArray.size() > 0)
			{
				parkingServiceManager.updateParking(jsonArray.getJSONObject(0));				
			}
			else
			{
				map.put("ok", "0");
				log.info("jsonArray is null....");
			}
			
		} 
		catch (Exception e) 
		{
			map.put("ok", "0");
			log.info(e.getMessage());
		}
		finally
		{
			JSONObject jsontip = JSONObject.fromObject(map);
			suc_info = jsontip.toString();
		}
		
		return SUCCESS;
	}
	
	public String removeParking()
	{
		Map<String,Object> map = new HashMap<String, Object>();
		Map<String,Object> inMap = new HashMap<String, Object>();
		
		try
		{
			JSONArray json = JSONArray.fromObject(inParam);
			
			if(json.size() > 0)			
			{
				inMap.put("parking", json);
				RetVO retVo = parkingServiceManager.removeParking(inMap);
				
				if (BaseConstants.RET_TRUE.equals(retVo.getResult()))
				{
					map.put("ok", "1");
				}
				else
				{
					map.put("ok", "0");
				}
				
			}
			else
			{
				map.put("ok", "0");
			}
			
		} 
		catch (Exception e) 
		{
			map.put("ok", "0");
			e.printStackTrace();
		}
		
		JSONObject jsontip = JSONObject.fromObject(map);
		suc_info = jsontip.toString();
		
		return SUCCESS;
	}
	
//	public String getParkingType(){
//		Map<String,Object> retMap = new HashMap<String, Object>();
//		Map<String,Object> paramMap = new HashMap<String, Object>();
//		try {
//			JSONObject json =JSONObject.fromObject(inParam);
//			if(!StrUtil.isNullOrEmpty(json.get("parkingTypeId"))){
//				paramMap.put("parkingTypeId", NumericUtil.nullToIntegerZero(json.get("parkingTypeId")));
//			}
//			if (!StrUtil.isNullOrEmpty(json.get("parkingTypeName"))){
//				paramMap.put("parkingTypeName", StrUtil.strnull(json.get("parkingTypeName")));
//			}
//			if (!StrUtil.isNullOrEmpty(json.get("communityId"))){
//				paramMap.put("communityId", NumericUtil.nullToIntegerZero(json.get("communityId")));
//			}
//			String type = StrUtil.strnull(json.get("qryType"));
//			RetVO retvo ;
//			
//			retvo = parkingServiceManager.getParkingType(paramMap,NumericUtil.nullToZero(page), BaseConstants.DEFAULT_PAGE_PER_COUNT);
//			
//			if (retvo != null && BaseConstants.RET_TRUE.equals(retvo.getResult())){
//				if (retvo.getObject() != null){
//					Map<String,Object> out = (Map<String,Object>)retvo.getObject();
//					JSONArray jsArray = (JSONArray)out.get("jsArray");
//					int pgcount = (Integer) out.get("pageCount");
//					retMap.put("data", jsArray);
//					retMap.put("pageCount", pgcount);
//				}
//				retMap.put("result", "true");
//				retMap.put("msg", "信息查询成功!");
//				
//			}else{
//				retMap.put("result", "false");
//				retMap.put("msg", "信息查询失败!");
//			}
//			
//			//JsonConfig jsonConfig = new JsonConfig();
//			
//			
//		} catch (Exception e) {
//			retMap.put("result", "false");
//			retMap.put("msg", "信息查询失败!");
//		}
//
//		JSONObject jsonArr = JSONObject.fromObject(retMap);
//		suc_info = jsonArr.toString();
//		return SUCCESS;
//	}
//	
//	public String saveParkingType(){
//		Map<String,Object> map = new HashMap<String, Object>();
//		Map<String,Object> inMap = new HashMap<String, Object>();
//		try {
//			JSONObject json =JSONObject.fromObject(inParam);
//			JSONArray jsonArray = json.getJSONArray("data");
//			if (jsonArray != null && jsonArray.size() > 0){
//				inMap.put("parkingType", jsonArray);
//				
//				RetVO retVo = parkingServiceManager.saveParkingType(inMap);
//				if (BaseConstants.RET_TRUE.equals(retVo.getResult())){
//					map.put("result", "true");
//					map.put("msg", "保存信息成功");
//				}else {
//					map.put("result", "false");
//					map.put("msg", "保存信息失败");
//				}
//			}
//			
//		} catch (Exception e) {
//			map.put("result", "false");
//			map.put("msg", "保存信息失败");
//			log.info(e.getMessage());
//		}
//		JSONObject jsontip = JSONObject.fromObject(map);
//		suc_info = jsontip.toString();
//		return SUCCESS;
//	}
//	
//	public String removeParkingType(){
//		Map<String,Object> map = new HashMap<String, Object>();
//		Map<String,Object> inMap = new HashMap<String, Object>();
//		try {
//			JSONArray json = JSONArray.fromObject(inParam);
//			if(json.size()>0){
//				inMap.put("parkingType", json);
//				RetVO retVo = parkingServiceManager.removeParkingType(inMap);
//				if (BaseConstants.RET_TRUE.equals(retVo.getResult())){
//					map.put("result", "true");
//					map.put("msg", "删除成功");
//				}else {
//					map.put("result", "false");
//					map.put("msg", "删除失败");
//				}
//			}else{
//				map.put("result", "false");
//				map.put("msg", "删除的信息不能为空");
//			}
//			
//		} catch (Exception e) {
//			map.put("result", "false");
//			map.put("msg", "删除失败");
//			e.printStackTrace();
//			log.info(e.getMessage());
//		}
//		JSONObject jsontip = JSONObject.fromObject(map);
//		suc_info = jsontip.toString();
//		return SUCCESS;
//	}
	
	public String search()
	{
		User user = (User) ActionContext.getContext().getSession().get("user");
		int userId = user.getUserId();
		
		JSONObject jsonObject = JSONObject.fromObject(inParam);
		String communityName = jsonObject.has("communityName") ? jsonObject.getString("communityName"):null;
		String parkingStatus = jsonObject.has("parkingStatus") ? jsonObject.getString("parkingStatus"):null;
		String parkingNbr = jsonObject.has("parkingNbr") ? jsonObject.getString("parkingNbr"):null;
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", userId);
		map.put("communityName", communityName);
		map.put("parkingStatus", parkingStatus);
		map.put("parkingNbr", parkingNbr);
		
		page = page == 0 ? 1:page;
		map.put("page", page);
		
		List<Object> rtnList = parkingServiceManager.getParkingSearchInfo(map);
		
		jsonObject.clear();
		jsonObject.put("found", 0);
		
		if (rtnList != null)
		{	
			PageInfo pageInfo = (PageInfo) rtnList.get(0);
			
			jsonObject.put("curPage", pageInfo.getCurrentPage());
			jsonObject.put("pageSize", pageInfo.getPerPageCount());
			jsonObject.put("totalRecs", pageInfo.getTotalCount());
			jsonObject.put("totalPages", pageInfo.getTotalPageCount());
			jsonObject.put("data", JSONArray.fromObject(pageInfo.getDataList()).toString());
			
			StatVO statVO = (StatVO) rtnList.get(1);
			jsonObject.put("total", statVO.getTotal());
			jsonObject.put("used", statVO.getUsed());
			jsonObject.put("unused", statVO.getUnused());
			
			jsonObject.put("found", 1);
		}
		
		suc_info = jsonObject.toString();
		
		log.debug(suc_info);
		
		return SUCCESS;
		
	}
}
